﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Common;
using ESOL.Common.Entity;

namespace ESOL.WinUI.MasterData
{
    public partial class frmMaintainProductionTypeQuery : BaseForm.frmBaseDialogyQueryForm
    {
        #region vars
        private MstProductTypeEntity mSelectedProductType;
        public MstProductTypeEntity SelectedProductType
        {
            get { return mSelectedProductType; }
        }

        private List<MstProductTypeEntity> mSelectedProductTypeList;
        public List<MstProductTypeEntity> SelectedProductTypeList
        {
            get { return mSelectedProductTypeList; }
        }

        private string mCategory;
        private bool mMulti = false;
        #endregion

        #region ctors
        public frmMaintainProductionTypeQuery()
        {
            InitializeComponent();
        }
        public frmMaintainProductionTypeQuery(bool multi)
        {
            InitializeComponent();
            mMulti = multi;
        }
        public frmMaintainProductionTypeQuery(ProductTypeCategory category)
        {
            InitializeComponent();
            mCategory = category.ToString();
        }
        #endregion

        #region functions
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbCategory, "CG", true);
            if (string.IsNullOrEmpty(mCategory) == false)
            {
                cmbCategory.SetSelectedValue(mCategory);
            }
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colCATEGORY, "CG", true);
            List<MstProductTypeEntity> list = MstProductTypeBs.GetProductTypeNotCG03();
            MstProductTypeEntity entity = new MstProductTypeEntity();
            list.Insert(0, entity);
            cmbParentType.DataSource = list;
            cmbParentType.DisplayMember = "ProductTypeName";
            cmbParentType.ValueMember = "ProductTypeId";
        }

        private string GetCondition()
        {
            string condition = "";
            if (cmbParentType.SelectedValue != null && string.IsNullOrEmpty(cmbParentType.SelectedValue.ToString()) == false)
            {
                condition += " AND PT1.PARENT_TYPE_ID = '" + cmbParentType.SelectedValue.ToString() + "' ";
            }
            if (string.IsNullOrEmpty(txtTypeCode.Text) == false)
            {
                condition += " AND PT1.PRODUCT_TYPE_CODE LIKE '%" + txtTypeCode.Text + "%' ";
            }
            if (string.IsNullOrEmpty(txtTypeName.Text) == false)
            {
                condition += " AND PT1.PRODUCT_TYPE_NAME LIKE '%" + txtTypeName.Text + "%' ";
            }
            if (string.IsNullOrEmpty(cmbCategory.Text) == false && cmbCategory.SelectedValue != null)
            {
                condition += " AND PT1.CATEGORY = '" + cmbCategory.SelectedValue + "' ";
            }
            return condition;
        }

        private void BindDataGrid()
        {
            string condition = GetCondition();
            string orderBy = "PRODUCT_TYPE_CODE";
            DataTable dt = MstProductTypeBs.GetByCondition(condition, orderBy, "");
            DataColumn dc=new DataColumn("SelectedFlag",typeof(bool));
            dc.DefaultValue=false;
            dt.Columns.Add(dc);
            gvMain.DataSource = dt;
        }
        #endregion

        #region events
        protected override void OnShown(EventArgs e)
        {
            BindComboBox();
            BindDataGrid();
            base.OnShown(e);
            if (mMulti)
            {
                gridView1.DoubleClick-=new EventHandler(gridView1_DoubleClick);
            }
            else
            {
                colSelected.Visible = false;
            }
        }

        DataRow drSelected;
        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            drSelected = gridView1.GetDataRow(e.FocusedRowHandle);
            mSelectedProductType = new MstProductTypeEntity(drSelected);
        }

        private void repositoryItemCheckEdit1_Click(object sender, EventArgs e)
        {
            if ((bool)drSelected["SelectedFlag"])
            {
                drSelected["SelectedFlag"] = false;

            }
            else
            {
                drSelected["SelectedFlag"] = true;
            }
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        protected override void Click_Confirm()
        {
            if (mMulti)
            {
                mSelectedProductTypeList = new List<MstProductTypeEntity>();
                DataTable dt = gvMain.DataSource as DataTable;
                foreach(DataRow dr in dt.Rows)
                {
                    if ((bool)dr["SelectedFlag"])
                    {
                        MstProductTypeEntity entity = new MstProductTypeEntity(dr);
                        mSelectedProductTypeList.Add(entity);
                    }
                }
                if (mSelectedProductTypeList.Count == 0)
                {
                    MessageBox.Show("请至少选择一个物品类别！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                if (mSelectedProductType == null)
                {
                    MessageBox.Show("请选择一个物品类别！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                this.DialogResult = DialogResult.OK;
            }
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            if (mSelectedProductType == null)
            {
                MessageBox.Show("请选择一个物品类别！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                this.DialogResult = DialogResult.OK;
            }
        }
        #endregion

    }
}
